<html>
<!-- =====================================================================

  File:      QueryNotifications.htm for Adventure Works Cycles Storefront Sample
  Summary:   Self-documentation for application
  Date:	     September 23, 2003

=====================================================================

  This file is part of the Microsoft SQL Server Code Samples.
  Copyright (C) Microsoft Corporation.  All rights reserved.

This source code is intended only as a supplement to Microsoft
Development Tools and/or on-line documentation.  See these other
materials for detailed information regarding Microsoft code samples.

THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
PARTICULAR PURPOSE.

======================================================= -->
    <head>
        <title>Adventure Works Cycles Store Documentation</title>
        <link rel="stylesheet" href="style.css">
    </head>
    <body class="NormalIndent">
        <h1>
            Using Query Notifications
        </h1>
        <b>Description:</b> Query notifications is a new feature in Microsoft&reg; SQL Server&trade; 2005 that provides client side events
        when the result of a query has changed due to an insert, update, or delete operation. This enables WinForm and WebForm based applications
        to maintain accurate caches of data, and thus dramatically reduces server load and increases application performance. The 
        ReviewWatcher sample application (typically located in 
        <i>drive</i>:\Program Files\Microsoft SQL Server\100\Samples\AdventureWorks\Storefront\ReviewWatcher) demonstrates this capability by issuing a query
        for all product reviews with a higher ID than the current ID that the application has processed. A dependency is associated
        with the query before it is executed. Part of registering the dependency includes providing an event handler to process the events generated 
        by the dependency. When a Storefront user adds a review for a product, a row is inserted in the ProductReview table. This triggers the query 
        notification, which then produces an event on the client. When the event is received a new query (with associated dependency) is issued by the sample application, 
        which also retrieves the new relevant data. This data is then used to update the user interface.
        <br>
        <br>
        <strong>Implementation Notes:</strong> The ReviewWatcher sample invokes the <strong>WatchForReviews</strong> method at startup, and after each
        event is fired. This method creates a command that accesses the ProductReview table, locating rows with ReviewIDs that are larger than those processed so
        far. Next, an instance of <strong>SqlDependency</strong> is created. This instance is associated with the <strong>SqlCommand</strong> 
        by passing the command as the first argument to the dependency's constructor. The SqlCommand needs to be a query which fits fairly strict guidelines. Next,
        the appropriate event handler (Dependency_OnChanged) is added to the <strong>OnChanged</strong> event for the dependency. Finally, the command is executed. 
        <br />
        <br />
        It is possible to get an event even though the data in the database may not have changed.
        It is important to design your event handler to correctly cope with that situation.
    </body>
</html>
